package com.qingxin.wuye.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyCarportArea;

@Service
public class WyCarportAreaService extends BaseService<WyCarportArea, String> {

	
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(String gardenid, LoginUser loginUser, boolean isParent){
		List<JsTreeNode> list = new ArrayList<>();

		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.wuye.entity.WyCarportArea(a.areaid, a.areaName)");
		hql.append(" from WyCarportArea a where a.wyGarden.gardenid=? ");
		hql.append(" order by a.areaName asc");
		
		List<WyCarportArea> areas = listByHql(hql.toString(), gardenid);
		if(areas != null){
			for(WyCarportArea area : areas){
				list.add(new JsTreeNode(area.getAreaid(), area.getAreaName(), SystemCfg.UI_DATATYPE_CARPORTAREA, null, isParent));
			}
		}
		return list;
	}
	
	
	public Pager list(String companyid, String gardenid, String areaName, PageParam pageParam, LoginUser loginUser, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.wuye.entity.WyCarportArea(" +
				"a.areaid, a.areaCode, a.areaName, a.remark, g.gardenName)" +
				" from WyCarportArea a, WyGarden g" +
				" where a.wyGarden.gardenid=g.gardenid ");
		
		if(StringUtils.isNotEmpty(companyid) && !"0".equals(companyid)){
			querier.append(" and g.wyPropertyCompany.companyid=:companyid ");
			querier.setParam("companyid", companyid);
		}
		
		if(StringUtils.isNotEmpty(gardenid) && !"0".equals(gardenid)){
			querier.append(" and a.wyGarden.gardenid=:gardenid ");
			querier.setParam("gardenid", gardenid);
		}
		
		if(StringUtils.isNotEmpty(areaName)){
			areaName = areaName.replace("'", "");
			querier.append(" and a.areaName like :areaName ");
			querier.setParam("areaName", "%"+areaName+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by a.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加信息
	 * @param carportArea
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyCarportArea carportArea, LoginUser loginUser, HttpServletRequest request){
		
		carportArea.setCreateTime(new Date());
		carportArea.setUpdateTime(new Date());
		carportArea.setCreateBy(loginUser.getLogin_name());
		carportArea.setUpdateBy(loginUser.getLogin_name());
		
		this.save(carportArea);
		
		return null;
	}
	
	
	/**
	 * 修改信息
	 * @param carportArea
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyCarportArea carportArea, LoginUser loginUser, HttpServletRequest request){
		WyCarportArea old = getSafeEntity(carportArea.getAreaid());
		
		String fields = "areaCode,areaName,remark";
		BeanUtil.setTargetFields(old, carportArea, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyCarportArea> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
	}
	
	
	/**
	 * 删除
	 * @param areaids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] areaids, LoginUser loginUser, HttpServletRequest request){
		for(String areaid : areaids){
			WyCarportArea carportArea = get(areaid);
			if(carportArea==null){
				continue;
			}
			Object obj = getByHql("select carportid from WyCarport where wyCarportArea.areaid=?", areaid);
			if(obj != null){
				return new SysError("车位区域“"+carportArea.getAreaName()+"”下存在车位档案信息，不可删除.");
			}
			this.delete(carportArea);
		}
		
		return null;
	}
	
}
